package com.github.fujianlian.klinechart.draw;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.github.fujianlian.klinechart.BaseKLineChartView;
import com.github.fujianlian.klinechart.R;
import com.github.fujianlian.klinechart.draw.main.BaseMainDraw;
import com.github.fujianlian.klinechart.entity.EntityGriddingInfo;
import com.github.fujianlian.klinechart.entity.h.ICandle;

import java.util.List;

/**
 * 主图的实现类
 * Created by tifezh on 2016/6/14.
 */
public class MainDraw<T> extends BaseMainDraw<T> {

    public MainDraw(BaseKLineChartView view) {
        super(view, Status.NONE.id);
    }

    @Override
    public void init(BaseKLineChartView view) {
        super.init(view);
    }

    @Override
    public void changgeColor(boolean changeColor) {
        super.changgeColor(changeColor);
        for (BaseMainDraw mainDraw : mainDrawList) {
            mainDraw.changgeColor(changeColor);
        }

    }

    /**
     * 绘制主图指标
     *
     * @param lastPoint
     * @param curPoint
     * @param lastX
     * @param curX
     * @param canvas
     * @param view
     * @param position
     */
    @Override
    public void drawTranslated(@Nullable ICandle lastPoint, @NonNull ICandle curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position) {
        //绘制主图指标和蜡烛图
        super.drawTranslated(lastPoint, curPoint, lastX, curX, canvas, view, position);

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            if (!baseMainDraw.isStatus(getStatus()) || baseMainDraw.getmZorder()>=0) {
                continue;
            }
            baseMainDraw.drawTranslated(lastPoint, curPoint, lastX, curX, canvas, view, position);
        }

        drawMain(lastPoint, curPoint, lastX, curX, canvas, view);

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            if (!baseMainDraw.isStatus(getStatus()) || baseMainDraw.getmZorder()<0) {
                continue;
            }
            baseMainDraw.drawTranslated(lastPoint, curPoint, lastX, curX, canvas, view, position);
        }

    }


    protected void drawMain(@Nullable ICandle lastPoint, @NonNull ICandle curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view) {
        if (isLine) {
            view.drawMainLine(canvas, mLinePaint, lastX, lastPoint.getClose(), curX, curPoint.getClose());
        } else {
            drawCandle(view, canvas, curX, curPoint, lastPoint);
        }

    }

    /**
     * 画Candle
     *
     * @param canvas
     * @param x      x轴坐标
     */
    protected void drawCandle(BaseKLineChartView view, Canvas canvas, float x, ICandle curPoint, ICandle lapoint) {
        boolean thesame = isPriceTheSame(curPoint);
        float v = thesame ? curPoint.getClose() / 3000 : 0;
        float high = curPoint.getHigh() + v, low = curPoint.getLow() - v, theopen = curPoint.getOpen() + v, theclose = curPoint.getClose() - v;

        high = view.getMainY(high);
        low = view.getMainY(low);
        float open = view.getMainY(theopen);
        float close = view.getMainY(theclose);
        float r = mCandleWidth / 2;
        float lineR = mCandleLineWidth / 2;
        Paint paint;

        if (theclose > theopen || (thesame && curPoint.getClose() > lapoint.getClose())) {
            paint = mRedPaint;
            //实心
            if (mCandleSolid) {
                canvas.drawRect(x - r, close, x + r, open, paint);
                canvas.drawRect(x - lineR, high, x + lineR, low, paint);
            } else {

                //上下影线
                paint.setStrokeWidth(mCandleLineWidth);
                canvas.drawLine(x, high, x, close, paint);
                canvas.drawLine(x, open, x, low, paint);
                //两侧竖线
                canvas.drawLine(x - r + lineR, open, x - r + lineR, close, paint);
                canvas.drawLine(x + r - lineR, open, x + r - lineR, close, paint);

                // 上下横线

                paint.setStrokeWidth(mCandleLineWidth * view.getScaleX());
                canvas.drawLine(x - r, open, x + r, open, paint);
                canvas.drawLine(x - r, close, x + r, close, paint);
            }

        } else if (theopen > theclose) {
            paint = mGreenPaint;
            if (mCandleSolid) {
                canvas.drawRect(x - r, open, x + r, close, paint);
                canvas.drawRect(x - lineR, high, x + lineR, low, paint);
            } else {
                paint.setStrokeWidth(mCandleLineWidth);
                canvas.drawLine(x, high, x, open, paint);
                canvas.drawLine(x, close, x, low, paint);

                //两侧竖线
                canvas.drawLine(x - r + lineR, open, x - r + lineR, close, paint);
                canvas.drawLine(x + r - lineR, open, x + r - lineR, close, paint);

                // 上下横线

                paint.setStrokeWidth(mCandleLineWidth * view.getScaleX());
                canvas.drawLine(x - r, open, x + r, open, paint);
                canvas.drawLine(x - r, close, x + r, close, paint);
//                canvas.drawRect(x - r, open, x + r, close, mGreenPaint);
//                canvas.drawRect(x - lineR, high, x + lineR, low, mGreenPaint);

            }
        } else {
            canvas.drawRect(x - r, open, x + r, close + 1, mRedPaint);
            canvas.drawRect(x - lineR, high, x + lineR, low, mRedPaint);

        }
    }


    public void drawGird(final BaseKLineChartView view, final Canvas canvas) {
        // 基准
        ICandle iCandle = view.getICandle();
        if (iCandle == null) {
            view.drawRowGrid(canvas);
            return;
        }

        view.drawGriddingLine(canvas, -999999999, iCandle.getStop_profit_point(), ContextCompat.getColor(view.getContext(), R.color.chart_ma10));
        view.drawGriddingLine(canvas, -999999999, iCandle.getStop_loss_point(), ContextCompat.getColor(view.getContext(), R.color.chart_ma5));


        final float standardPrice = iCandle.getStandardPrice();
        if (standardPrice <= 0) {
            view.drawRowGrid(canvas);
            return;
        }

        view.drawGriddingLine(canvas, 0, standardPrice, -1);
    }


    public void drawGirdText(BaseKLineChartView view, Canvas canvas) {
        // 基准
        ICandle iCandle = view.getICandle();
        if (iCandle == null) return;
        view.drawGriddingLine(canvas, 0, iCandle.getStandardPrice(), -1);
        if (iCandle.getAllGriddingInfos() != null)
            for (EntityGriddingInfo allGriddingInfo : iCandle.getAllGriddingInfos()) {
                view.drawGriddingLine(canvas, allGriddingInfo.getIndex(), (float) allGriddingInfo.getPrice(), changeColor ? Color.GRAY : -1);
            }
    }

    @Override
    public void drawTranslateds(Canvas canvas, BaseKLineChartView view, List<ICandle> datas, int mStartIndex, int mStopIndex) {
        if (!isLine) return;

        float[] pts = new float[(mStopIndex - mStartIndex + 1) * 4];

        for (int i = mStartIndex; i <= mStopIndex; i++) {
            ICandle currentPoint = (ICandle) view.getItem(i);
            float currentPointX = view.getX(i);
            ICandle lastPoint = i == 0 ? currentPoint : (ICandle) view.getItem(i - 1);
            float lastX = i == 0 ? currentPointX : view.getX(i - 1);

            pts[(i - mStartIndex) * 4] = lastX;
            pts[(i - mStartIndex) * 4 + 1] = view.getMainY(lastPoint.getClose());
            pts[(i - mStartIndex) * 4 + 2] = currentPointX;
            pts[(i - mStartIndex) * 4 + 3] = view.getMainY(currentPoint.getClose());
        }

        long l = System.currentTimeMillis();
//        view.drawLines(canvas, mLinePaint, pts);

//        Log.e("chart", "耗时 " + (System.currentTimeMillis() - l));
    }

    /**
     * 绘制文本提示
     *
     * @param canvas
     * @param view
     * @param position 该点的位置
     * @param x        x的起始坐标
     * @param y        y的起始坐标
     */
    @Override
    public void drawText(@NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position, float x, float y) {
        y = y - 5;

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            if (!baseMainDraw.isStatus(getStatus())) {
                continue;
            }

            baseMainDraw.drawText(canvas, view, position, x, y);
        }
    }


    /**
     * 设置ma5颜色
     *
     * @param color
     */
    public void setMa5Color(int color) {

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setMa5Color(color);
        }
    }

    /**
     * 设置ma10颜色
     *
     * @param color
     */
    public void setMa10Color(int color) {

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setMa10Color(color);
        }
    }

    /**
     * 设置ma30颜色
     *
     * @param color
     */
    public void setMa30Color(int color) {

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setMa30Color(color);
        }
    }

    /**
     * 设置选择器文字颜色
     *
     * @param color
     */
    public void setSelectorTextColor(int color) {
        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setSelectorTextColor(color);
        }

    }

    /**
     * 设置选择器文字大小
     *
     * @param textSize
     */
    public void setSelectorTextSize(float textSize) {

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setSelectorTextSize(textSize);
        }
    }

    /**
     * 设置选择器背景
     *
     * @param color
     */
    public void setSelectorBackgroundColor(int color) {

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setSelectorBackgroundColor(color);
        }
    }

    /**
     * 设置曲线宽度
     */
    public void setLineWidth(float width) {
        super.setLineWidth(width);

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setStrokeWidth(width);
        }
    }

    /**
     * 设置文字大小
     */
    public void setTextSize(float textSize) {

        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setTextSize(textSize);
        }
    }


    public void setLine(boolean line) {
        super.setLine(line);
        if (isLine != line) {
            for (BaseMainDraw baseMainDraw : mainDrawList) {
                baseMainDraw.setLine(isLine);
            }
        }
    }

    public void setStatus(Status.Sta status) {
        super.setStatus(status);
        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setStatus(status);
        }
    }

    @Override
    public void setCandleLineWidth(float candleLineWidth) {
        super.setCandleLineWidth(candleLineWidth);
        for (BaseMainDraw baseMainDraw : mainDrawList) {
            baseMainDraw.setCandleLineWidth(candleLineWidth);
        }
    }
}
